﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace WebMVC.Controllers
{
    public class ChiTietNhapXuatController : Controller
    {
        databaseDataContext db = new databaseDataContext();
        //
        // GET: /ChiTietNhapXuat/
        [Authorize(Roles = "0")]
        public ActionResult Index()
        {
            ViewData["Items"] = from h in db.ChiTietNhapXuats
                                select h;
            return View();
        }

        //
        // GET: /ChiTietNhapXuat/Details/5
        [Authorize(Roles = "0")]
        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /ChiTietNhapXuat/Create
        [Authorize(Roles = "0")]
        public ActionResult Create()
        {
            IEnumerable<SelectListItem> listPhieuNhapXuat = db.PhieuNhapXuats.Select(c => new SelectListItem
            {
                Value = c.ID.ToString(),
                Text = c.Ki_hieu.ToString()
            });

            IEnumerable<SelectListItem> listHangHoa = db.HangHoas.Select(c => new SelectListItem
            {
                Value = c.ID.ToString(),
                Text = c.Ten.ToString()
            });

            ViewData["ListPhieuNhapXuat"] = listPhieuNhapXuat;
            ViewData["ListHangHoa"] = listHangHoa;
            return View();
        } 

        //
        // POST: /ChiTietNhapXuat/Create
        [Authorize(Roles = "0")]
        [HttpPost]
        public ActionResult Create(ChiTietNhapXuat chitietnhapxuat)
        {
            try
            {
                // TODO: Add insert logic here

                chitietnhapxuat.Ngay_cap_nhat_cuoi = DateTime.Now;
                db.ChiTietNhapXuats.InsertOnSubmit(chitietnhapxuat);
                db.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                IEnumerable<SelectListItem> listPhieuNhapXuat = db.PhieuNhapXuats.Select(c => new SelectListItem
                {
                    Value = c.ID.ToString(),
                    Text = c.Ki_hieu.ToString()
                });

                IEnumerable<SelectListItem> listHangHoa = db.HangHoas.Select(c => new SelectListItem
                {
                    Value = c.ID.ToString(),
                    Text = c.Ten.ToString()
                });

                ViewData["ListPhieuNhapXuat"] = listPhieuNhapXuat;
                ViewData["ListHangHoa"] = listHangHoa;
                return View();
            }
        }
        
        //
        // GET: /ChiTietNhapXuat/Edit/5
        [Authorize(Roles = "0")]
        public ActionResult Edit(int id)
        {
            ChiTietNhapXuat model = (from m in db.ChiTietNhapXuats
                             where m.ID == id
                             select m).SingleOrDefault();
            IEnumerable<SelectListItem> listPhieuNhapXuat = db.PhieuNhapXuats.Select(c => new SelectListItem
            {
                Value = c.ID.ToString(),
                Text = c.Ki_hieu.ToString()
            });

            IEnumerable<SelectListItem> listHangHoa = db.HangHoas.Select(c => new SelectListItem
            {
                Value = c.ID.ToString(),
                Text = c.Ten.ToString()
            });

            ViewData["ListPhieuNhapXuat"] = listPhieuNhapXuat;
            ViewData["ListHangHoa"] = listHangHoa;
            return View(model);
        }

        //
        // POST: /ChiTietNhapXuat/Edit/5
        [Authorize(Roles = "0")]
        [HttpPost]
        public ActionResult Edit(int id, ChiTietNhapXuat chitietnhapxuat)
        {
            ChiTietNhapXuat model = (from m in db.ChiTietNhapXuats
                                     where m.ID == id
                                     select m).SingleOrDefault();
            try
            {
                // TODO: Add update logic here
                model.Ma_phieu_nhap_xuat = chitietnhapxuat.Ma_phieu_nhap_xuat;
                model.Ma_hang_hoa = chitietnhapxuat.Ma_hang_hoa;
                model.So_luong = chitietnhapxuat.So_luong;
                model.Gia = chitietnhapxuat.Gia;
                model.Thanh_tien = chitietnhapxuat.Thanh_tien;
                model.Thue = chitietnhapxuat.Thue;
                model.Tien_thue = chitietnhapxuat.Tien_thue;
                model.Tong_tien = chitietnhapxuat.Tong_tien;
                model.Chiet_khau = chitietnhapxuat.Chiet_khau;
                model.Tien_chiet_khau = chitietnhapxuat.Tien_chiet_khau;
                model.Han_su_dung = chitietnhapxuat.Han_su_dung;
                model.Bao_hanh = chitietnhapxuat.Bao_hanh;
                model.Phi_van_chuyen = chitietnhapxuat.Phi_van_chuyen;
                model.So_luong_goc = chitietnhapxuat.So_luong_goc;
                model.Ghi_chu = chitietnhapxuat.Ghi_chu;
                model.Ngay_cap_nhat_cuoi = DateTime.Now;
                db.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                IEnumerable<SelectListItem> listPhieuNhapXuat = db.PhieuNhapXuats.Select(c => new SelectListItem
                {
                    Value = c.ID.ToString(),
                    Text = c.Ki_hieu.ToString()
                });

                IEnumerable<SelectListItem> listHangHoa = db.HangHoas.Select(c => new SelectListItem
                {
                    Value = c.ID.ToString(),
                    Text = c.Ten.ToString()
                });

                ViewData["ListPhieuNhapXuat"] = listPhieuNhapXuat;
                ViewData["ListHangHoa"] = listHangHoa;
                return View(model);
            }
        }

        //
        // GET: /ChiTietNhapXuat/Delete/5
        [Authorize(Roles = "0")]
        public ActionResult Delete(int id)
        {
            var model = (from h in db.ChiTietNhapXuats
                         where h.ID == id
                         select h).Take(1).SingleOrDefault();
            db.ChiTietNhapXuats.DeleteOnSubmit(model);
            db.SubmitChanges();
            return RedirectToAction("Index");
        }

        //
        // POST: /ChiTietNhapXuat/Delete/5
        [Authorize(Roles = "0")]
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
